/*
 * OPIAM Suite
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */

package opiam.admin.faare.config;

import opiam.admin.faare.config.javabeans.JBAcls;

import org.apache.commons.digester.Digester;

import org.apache.log4j.Logger;

import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

import java.io.IOException;
import java.io.InputStream;


/**
 * ACLs service configuration loading from the XML file.
 *
 * The XML file must contain the following lines :
 * <!DOCTYPE mapping PUBLIC
 *         "-//OPIAM ADMIN//DTD ACL Service Configuration 1.0//EN"
 *         "http://www.opiam.org/admin/dtd/acl_conf.dtd">
 *
 * The second line is registered to the digester (it does not need to be an actual URL).
 *
 */
public final class AclsConfig implements ErrorHandler
{
    /** Log4J. */
    private static Logger _logger = Logger.getLogger(AclsConfig.class.getName());

    /** DOCTYPE key. */
    public static final String ACL_CONFIG_DTD_KEY = "-//OPIAM ADMIN//DTD ACL Service Configuration 1.0//EN";

    /** DOCTYPE URL. */
    public static final String ACL_CONFIG_DTD_URL = "/config/service/acl/acl_conf.dtd";

    /** XML Parser. */
    private static Digester _digester = null;

    /** Current instance. */
    private static AclsConfig _instance = null;

    /** Parser exception. */
    private static SAXParseException _saxParseException = null;

    /**
     * Constructor for ProfilesDigester.
     */
    private AclsConfig()
    {
        super();
    }

    /**
     * Digester initialization with DTD fields.
     */
    private static void initDigester()
    {
        _saxParseException = null;
        _digester = new Digester();

        _digester.setErrorHandler(_instance);
        _digester.setValidating(true);

        _digester.register(ACL_CONFIG_DTD_KEY,
            AclsConfig.class.getResource(ACL_CONFIG_DTD_URL).toString());
        _digester.addObjectCreate("acls",
            "opiam.admin.faare.config.javabeans.JBAcls");
        _digester.addSetProperties("acls");

        _digester.addObjectCreate("acls/acl",
            "opiam.admin.faare.config.javabeans.JBAcl");
        _digester.addSetNext("acls/acl", "addAcl",
            "opiam.admin.faare.config.javabeans.JBAcl");
        _digester.addSetProperties("acls/acl");

        _digester.addCallMethod("acls/acl/modattr", "addModAttr", 1);
        _digester.addCallParam("acls/acl/modattr", 0);

        _digester.addCallMethod("acls/acl/param", "addParam", 2);
        _digester.addCallParam("acls/acl/param/param-name", 0);
        _digester.addCallParam("acls/acl/param/param-value", 1);
    }

    /**
     * Gets AclsConfig instance.
     *
     * @return AclsConfig instance
     */
    public static AclsConfig getInstance()
    {
        if (_instance == null)
        {
            _instance = new AclsConfig();
            initDigester();
        }

        return _instance;
    }

    /**
     * Gets ACLs config.
     *
     * @param in configuration stream
     *
     * @return ACLs config
     *
     * @throws IOException exception while reading stream.
     * @throws SAXException exception while parsing data.
     */
    public JBAcls readConfig(InputStream in) throws IOException, SAXException
    {
        JBAcls result = null;
        result = (JBAcls) _digester.parse(in);

        if (_saxParseException != null)
        {
            _logger.debug("Exception : " + _saxParseException.getMessage());
            throw new SAXException(_saxParseException.getMessage());
        }

        return result;
    }

    /**
     * Parsing error callback.
     *
     * @param saxParseException parsing exception
     */
    @Override
	public void error(SAXParseException saxParseException)
    {
        _logger.debug(" error exception!!!!!!!!!!!!!!!");
        _saxParseException = saxParseException;
    }

    /**
     * Parsing fatal error callback.
     *
     * @param saxParseException parsing exception
     */
    @Override
	public void fatalError(SAXParseException saxParseException)
    {
        _logger.debug("fatalError exception!!!!!!!!!!!!!!!");
        _saxParseException = saxParseException;
    }

    /**
     * Parsing warning callback.
     *
     * @param saxParseException parsing exception
     */
    @Override
	public void warning(SAXParseException saxParseException)
    {
        _logger.debug("warning : " + saxParseException.getMessage());
    }
}
